define(function(require, exports, module) {
    require('basic');
    var config = require('config');
    var model = function(){
        this.init();
    };
    model.prototype ={
        info: {
            token: null,
            leadPost: null,
            checkInfoBL: false
        },
        login: function(){
            var sf = this;
            var loginForm = $('#loginForm'),
                obj = {};
            loginForm.find('.form-control').each(function(index,el){
                obj[$(el).attr('name')] = $(el).val();
            });
            var postData = JSON.stringify(obj);
            $.ajax({
                type: "POST",
                url: config.login,
                data: postData,
                dataType: 'json',
                success: function(data){
                    if(data){
                        var now = new Date();
                        $.cookie('_login_date',now,{path:'/'});
                        $.cookie('_username',obj['username'],{path:'/'}); //设置username的cookie
                        sf.setInfo(data);
                        sf.hideLoginBox();
                        seajs.use('main');
                    };
                },
                error: function(e){
                    $('#error_body').html('').html(e.responseText);
                    $('#error').modal({keyboard: false, show: true});
                }
            });
        },
        logout: function(){
            var sf = this;
            $.ajax({
                type: "POST",
                url: config.login_exit+this.info.leadPost,
                success: function(data){
                    $.cookie('_username', null,{path:'/'});
                    $.cookie('_login_date', null,{path:'/'});
                    $.cookie('_token', null,{path:'/'});
                    $.cookie('_dc', null,{path:'/'});
                    location.reload(true);
                },
                error: function(e){
                    $('#error_body').html('').html(e.responseText);
                    $('#error').modal({keyboard: false, show: true});
                }
            });

        },
        setInfo: function(_str){
            var sf = this;
            this.info.token = _str;
            this.info.leadPost = '?token='+_str;
            this.info.checkInfoBL = true;
            $.cookie('_token',_str,{path:'/'});
            if($.cookie('_username')){
                $('.username_view').text($.cookie('_username'));
            };
        },
        checkInfo: function(){
            var sf = this;
            var token = $.cookie('_token') || this.info.token;
            if(token){
                var data_str = '?token='+token;
                sf.checkAjax = $.ajax({
                    type: "GET",
                    url: config.login_check+data_str,
                    dataType: 'json',
                    success: function(data){
                        if(data){
                            sf.setInfo(token);
                            seajs.use('main');
                        }else{
                            //sf.showLoginBox();
                            window.location.href = 'login.php';
                        };
                    } 
                });
            }else{
                $.removeCookie('_dc');
                //this.showLoginBox();
                window.location.href = 'login.php';
            };
        },
        showLoginBox: function(){
            var sf = this;
            $('#poplogin').modal({keyboard: false, show: true});
            $('#loginForm #submit').on('click', function(e){
                sf.login();
            });
            $('#password').on('keydown',function(e){
                if(e.keyCode===13){
                    sf.login();
                };
            });
        },
        hideLoginBox: function(){
            var sf = this;
            $('#poplogin').modal('hide');
        },
        init: function(){
            var sf = this;
            this.checkInfo();
            $('#logout').on('click', function(){
                sf.logout();
            });
        }
    };
    module.exports = new model();
});

